//microcommand define h2gwo
let pinyin;
signature("<text:rawtext>", {
    checkUnknown: true
});
option("separator", "-p <char>", { descPath: "commands.h2p.options.separator" });
option("separator", "-d", { value: "dash", descPath: "commands.h2p.options.separator.dash" });
option("separator", "-_", { value: "underscore", descPath: "commands.h2p.options.separator.underscore" });
option("separator", "-P, --no-separator", { value: "none", descPath: "commands.h2p.options.separator.none" });
option("surname", "-s", { descPath: "commands.h2p.options.surname" });
option("surname", "-S, --surname-all", { value: "all", descPath: "commands.h2p.options.surname.all" });
action(({
    options: {
        separator,
        surname,
    }
}, t) => h.text((pinyin ??= require("pinyin-pro").pinyin)(t, {
    nonZh: "consecutive",
    surname: surname === true ? "head" : surname || "off",
    type: "all",
    toneType: "none",
}).reduce((a, c, i, l) => a + (c.pinyin && l[i - 1]?.pinyin ? {
    dash: "-",
    underscore: "_",
    none: ""
} [separator] ?? separator ?? " " : (c.pinyin && l[i - 1]?.origin.match(/[\p{L}\p{N}]$/u)) || (l[i - 1]?.pinyin && c.origin.match(/^[\p{L}\p{N}]/u)) ? " " : "") + (c.pinyin ? ((p, t) => {
    if (t > 2)
        p = p.replace(/^i/, 'yi').replace(/^u/, 'wu')
    if (t == 4)
        p = p.replace(/^yi([aeu])/, 'y$1').replace(/^wu([ae])/, 'w$1')
    for (let [r, b] of [
        [],
        [
            [/^([lmnr])(.+)$/, '$1h$2'],
        ],
        [
            [/^([lmnr])(.+)$/, '$1$2'],
            [/^([^ae]*)i$/, '$1yi'],
            [/^([^ao]*)u$/, '$1wu'],
            [/^([^ae]*)i(.+)$/, '$1y$2'],
            [/^([^ao]*)u(.+)$/, '$1w$2'],
            [/([aeiouy]+)(.*)$/, '$1r$2'],
        ],
        [
            [/^([^aeiou]*)([iu])(ng?)?$/, '$1$2$2$3'],
            [/^([^u]*)i(.*)$/, '$1e$2'],
            [/^(.*)u(.*)$/, '$1o$2'],
            [/([aeiouy])(.*)$/, '$1$1$2'],
        ],
        [
            [/^([^ae]*)i$/, '$1ih'],
            [/^([^ao]*)u$/, '$1uh'],
            [/i$/, 'y'],
            [/u$/, 'w'],
            [/l$/, 'll'],
            [/ng$/, 'nq'],
            [/n$/, 'nn'],
            [/$/, 'h'],
        ],
    ][t]) {
        if (p.match(r))
            return p.replace(r, b)
    }
    return p;
})([
    [/^r$/, 'er'],
    ['iu', 'iou'],
    [/(?<!y)u([in])/, 'ue$1'],
    ['ao', 'au'],
    [/^yi?/, 'i'],
    [/^wu?/, 'u'],
    [/^([jqx])u/, '$1iu'],
    ['ü', 'iu'],
    [/^([zcsr]h?)i/, '$1y'],
    [/^zh/, 'j'],
    [/^z/, 'tz'],
    [/^c(?!h)/, 'ts'],
    [/^q/, 'ch'],
    [/^x/, 'sh'],
    ['er', 'el'],
].reduce(((l, [r, b]) => l.replace(r, b)), c.pinyin), c.num || 0) : c.origin), "")));
locale("zh-CN", { description: "汉字转国语罗马字" })